<template>
  <a-modal
    title="批量申请授权"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          label="项目名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select :showSearch="true" style="width: 100%" placeholder="请选择项目名"
                    v-decorator="['projectName', {rules: [{ required: true, message: '请选择项目名！' }]}]">
            <a-select-option v-for="(item,index) in grantInfo.projects" :key="index" :value="item.id">{{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="甲方单位"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select :showSearch="true" style="width: 100%" placeholder="请选择甲方单位"
                    v-decorator="['unitName', {rules: [{ required: true, message: '请选择甲方单位！' }]}]">
            <a-select-option v-for="(item,index) in grantInfo.parties" :key="index" :value="item.id">{{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="地区"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入地区" v-decorator="['region', {rules: [{required: true, message: '请输入地区！'}]}]"/>
        </a-form-item>

        <a-form-item
          label="所属代理商"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入所属代理商" v-decorator="['agent', {rules: [{required: true, message: '请输入所属代理商！'}]}]"/>
        </a-form-item>

        <a-form-item
          label="授权时长（月）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input-number :min="0" style="width: 100%" placeholder="请输入授权时长（数字）"
                          v-decorator="['authorizedTime', {rules: [{required: true, message: '请输入授权时长（数字）！'}]}]"/>
        </a-form-item>

        <a-form-item
          label="授权类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select :showSearch="true" style="width: 100%" placeholder="请选择授权类型"
                    v-decorator="['type', {rules: [{ required: true, message: '请选择授权类型！' }]}]">
            <a-select-option v-for="(item,index) in grantInfo.grantTypes" :key="index" :value="item.id">{{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="授权设备类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select :showSearch="true" style="width: 100%" placeholder="请选择授权设备类型"
                    v-decorator="['authorizedDeviceType', {rules: [{ required: true, message: '请选择授权设备类型！' }]}]">
            <a-select-option v-for="(item,index) in grantInfo.grantDeviceTypes" :key="index" :value="item.id">{{
              item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-textarea :rows="4" placeholder="请输入备注"
                      v-decorator="['remark', {rules: [{required: false, message: '请输入备注！'}]}]"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { batchAddApplicationRecord, queryGrantInfo } from '@/api/modular/main/bizgateway/bizGatewayManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        treeLoading: true,
        form: this.$form.createForm(this),
        grantInfo: {},
        gatewayCodes: null
      }
    },
    methods: {
      init (gatewayCodes) {
        const that = this
        that.visible = true
        this.gatewayCodes = gatewayCodes
        queryGrantInfo().then(res => {
          if (res.success) {
            this.grantInfo = res.data
          }
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        validateFields((errors, values) => {
          if (!errors) {
            values.deviceCodeList = this.gatewayCodes
            this.confirmLoading = true
            batchAddApplicationRecord(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('批量申请授权成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('批量申请授权失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
        this.gatewayCodes = null
      }
    }
  }
</script>
